﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PESWeb.Resources;
using System.Text;
using System.Web.UI;
using System.IO;
using System.Web.UI.WebControls;
using PESWeb.Pages.Controls;

namespace PESWeb
{
    using PES.Model;

    public class BasePage : System.Web.UI.Page
    {
		public Campeonato CampeonatoCarregado
        {
            get
            {
                if (Session == null)
                {
                    Response.Redirect("~/Default.aspx");
                }

                object campeonato = Session[ResSessionKeys.CAMPEONATO_CARREGADO];
                if (campeonato == null)
                {
                    Response.Redirect("~/Default.aspx");
                    return null;
                }
                else
                {
                    return (Campeonato)campeonato;
                }
            }

            set
            {
                if (Session == null)
                {
                    Response.Redirect("~/Default.aspx");
                }
                Session[ResSessionKeys.CAMPEONATO_CARREGADO] = value;
            }
        }

        public IList<Campeonato> CampeonatosCarregados
        {
            get
            {
                if (Session == null)
                {
                    Response.Redirect("~/Default.aspx");
                }

                object campeonatos = Session[ResSessionKeys.CAMPEONATOS_ANDAMENTO];
                if (campeonatos == null)
                {
                    Response.Redirect("~/Default.aspx");
                    return null;
                }
                else
                {
                    return (List<Campeonato>)campeonatos;
                }
            }

            set
            {
                if (Session == null)
                {
                    Response.Redirect("~/Default.aspx");
                }
                Session[ResSessionKeys.CAMPEONATOS_ANDAMENTO] = value;
            }
        }

		private string _currentNotification = string.Empty;

		public string Notification
		{
			get
			{
				return _currentNotification;
			}
			set
			{
				_currentNotification = value;
				PopupNotification notificationControl = Master.FindControl("ctrlPopupNotification") as PopupNotification;
				notificationControl.Text = _currentNotification;

				if (_currentNotification != string.Empty)
					notificationControl.Visible = true;
				else
					notificationControl.Visible = false;
			}
		}

        public void ChangeFacebookMetaInfo(string _contentPlaceHolderID, params string[] args) 
        {
            //Ajusta o link do like do facebook
            Control htmlHeaderControl = this.Page.Header.FindControl(_contentPlaceHolderID);
            if (htmlHeaderControl != null &&
                htmlHeaderControl is ContentPlaceHolder)
            {
                ContentPlaceHolder htmlHeaderContentPlaceHolder = (ContentPlaceHolder)htmlHeaderControl;
                //procura pelo controle que tem 
                foreach (var control in htmlHeaderControl.Controls)
                {
                    if (control is LiteralControl)
                    {
                        LiteralControl literalMetaControl = (LiteralControl)control;
                        //Verifica se é a metainfo do facebook mesmo
                        if (literalMetaControl.Text.Contains("og:url"))
                        {
                            literalMetaControl.Text = string.Format(literalMetaControl.Text, args);
                        }
                    }
                }
            }
        }

        private static string[] aspNetFormElements = 
            new string[] { "__EVENTTARGET", "__EVENTARGUMENT", "__VIEWSTATE", "__EVENTVALIDATION", "__VIEWSTATEENCRYPTED" };

        protected override void Render(HtmlTextWriter writer)
        {
            StringWriter stringWriter = new StringWriter();
            HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
            base.Render(htmlWriter);
            string html = stringWriter.ToString();
            int formStart = html.IndexOf("<form");
            int endForm = -1;
            if (formStart >= 0)
                endForm = html.IndexOf(">", formStart);

            if (endForm >= 0)
            {
                StringBuilder viewStateBuilder = new StringBuilder();
                foreach (string element in aspNetFormElements)
                {
                    int startPoint = html.IndexOf("<input type=\"hidden\" name=\"" + element + "\"");
                    if (startPoint >= 0 && startPoint > endForm)
                    {
                        int endPoint = html.IndexOf("/>", startPoint);
                        if (endPoint >= 0)
                        {
                            endPoint += 2;
                            string viewStateInput = html.Substring(startPoint, endPoint - startPoint);
                            html = html.Remove(startPoint, endPoint - startPoint);
                            viewStateBuilder.Append(viewStateInput).Append("\r\n");
                        }
                    }
                }

                if (viewStateBuilder.Length > 0)
                {
                    viewStateBuilder.Insert(0, "\r\n");
                    html = html.Insert(endForm + 1, viewStateBuilder.ToString());
                }
            }

            writer.Write(html);
        }
    }
}